import logging

from odoo import models, fields, api, _
from odoo.exceptions import UserError, ValidationError

_logger = logging.getLogger(__name__)


class DouyinAuthor(models.Model):
    """抖音创作者模型
    """
    _name = 'douyin.author'
    _description = 'Douyin Author'
    _rec_name = 'nickname'

    nickname = fields.Char(string='昵称')
    uid = fields.Char(string='UID')
    avatar_thumb = fields.Char(string='头像')
    aweme_count = fields.Integer(string='作品数量')
    follower_count = fields.Integer(string='粉丝数量')
    signature = fields.Text(string='签名')
    # https://www.douyin.com/user/{sec_uid}可以访问作者主页
    sec_uid = fields.Char(string='Sec UID')
    collection_ids = fields.One2many(comodel_name='douyin.collection', inverse_name='author_id', string='合集')
    author_url = fields.Char(string='作者主页', compute='_compute_author_url')

    _sql_constraints = [
        ('uid_uniq', 'unique (uid)', 'UID是唯一的')
    ]

    @api.depends('sec_uid')
    def _compute_author_url(self):
        for rec in self:
            rec.author_url = f'https://www.douyin.com/user/{rec.sec_uid}'
